package com.paypalpay.timingtask;

import com.paypalpay.entity.common.DeviceRealTimeInformation;
import com.paypalpay.entity.common.UserboundPaymentOrder;
import com.paypalpay.pojo.Constant;
import com.paypalpay.service.PartnerService;
import com.paypalpay.service.UnifiedOrderPackagesService;
import com.paypalpay.service.UserboundPaymentOrderService;
import com.paypalpay.turntointerfaceservice.TurnToRenewPackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class RenewPackageTask {
    @Autowired
    private UserboundPaymentOrderService userboundPaymentOrderService;
    @Autowired
    private UnifiedOrderPackagesService unifiedOrderPackagesService;
    @Autowired
    PartnerService partnerService;
    //每小时执行一次
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void synchronizationTask() {
        try {
              // 2,生成当前时间戳
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String curtime = df.format(new Date());
            //在数据库获取数据
            System.out.println("RenewPackageTask续订定时任务正在执行！");
            List<UserboundPaymentOrder> userList = userboundPaymentOrderService.getListToRenewPackage();
            if (userList == null || userList.isEmpty()) {
                System.out.println("没有查询到需要去续订的数据！");
            } else {
                for (UserboundPaymentOrder order : userList) {
                    String orderId = order.getOrderId();
                    String orderPeriod = order.getOrderPeriod();
                    String deviceId = order.getDeviceId();
                    String deviceType = order.getDeviceType();
                    String partnerCode = order.getPartnerCode();
                    String secret = partnerService.findSecretByPartnerCode(partnerCode);

                    TurnToRenewPackagesService renewPackage = new TurnToRenewPackagesService();
                    DeviceRealTimeInformation information = renewPackage.renewPackage(orderId, deviceType, orderPeriod, secret, partnerCode);

                    if (information == null || !true == Constant.RET_CODE_0000.equalsIgnoreCase(information.getRetCode())) {
                        System.out.println("续订失败：" + information.getResultInfo());

                    } else if (true == Constant.RET_CODE_0000.equalsIgnoreCase(information.getRetCode())) {
                        //续订成功
                        System.out.println("续订成功：" + information.getResultInfo());

                        String order_id = information.getOrderCode();
                        String order_expire_date =information.getExpireDate();

                        //将数据记录状态改一下
                        UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
                        //续订成功
                        paymentOrder.setIsRenew("0");
                        paymentOrder.setOrderId(order_id);
                        userboundPaymentOrderService.updateIsRenew(paymentOrder);
                        System.out.println("续订结果入库成功");

                    }


                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
